<template>
  <div class="skill-detail-container" v-loading="loading">
    <div class="detail-content" v-if="skill">
      <div class="left-section">
        <div class="cover">
          <img :src="skill.coverImage || '/default-skill.jpg'" alt="">
        </div>
        <div class="info-card">
          <h1>{{ skill.title }}</h1>
          <div class="tags">
            <el-tag v-for="(tag, index) in parseTags(skill.tags)" :key="index">
              {{ tag }}
            </el-tag>
          </div>
          <div class="stats">
            <div class="stat-item">
              <el-icon><View /></el-icon>
              <span>{{ skill.viewCount }} 浏览</span>
            </div>
            <div class="stat-item">
              <el-icon><Star /></el-icon>
              <span>{{ skill.favoriteCount }} 收藏</span>
            </div>
            <div class="stat-item">
              <el-icon><Calendar /></el-icon>
              <span>{{ skill.appointmentCount }} 预约</span>
            </div>
          </div>
        </div>
      </div>

      <div class="right-section">
        <div class="description-card">
          <h2>技能描述</h2>
          <p>{{ skill.description }}</p>
        </div>

        <div class="user-card">
          <h3>发布者信息</h3>
          <div class="user-info">
            <el-avatar :size="50" :src="skill.userAvatar">
              {{ skill.userNickname?.[0] }}
            </el-avatar>
            <div class="user-details">
              <div class="nickname">{{ skill.userNickname }}</div>
              <div class="username">@{{ skill.username }}</div>
            </div>
          </div>
          <el-button 
            type="primary" 
            plain 
            size="small" 
            @click="startChat"
            style="margin-top: 10px; width: 100%"
          >
            <el-icon><ChatDotRound /></el-icon>
            发消息
          </el-button>
        </div>

        <div class="action-buttons">
          <el-button type="primary" size="large" @click="showAppointmentDialog = true">
            <el-icon><Calendar /></el-icon>
            立即预约
          </el-button>
          <el-button :type="skill.isFavorited ? 'warning' : 'default'" size="large" @click="toggleFavorite">
            <el-icon><Star /></el-icon>
            {{ skill.isFavorited ? '已收藏' : '收藏' }}
          </el-button>
        </div>
      </div>
    </div>

    <!-- 预约对话框 -->
    <el-dialog v-model="showAppointmentDialog" title="创建预约" width="500px">
      <el-form :model="appointmentForm" label-width="100px">
        <el-form-item label="预约时间">
          <el-date-picker
            v-model="appointmentForm.appointmentTime"
            type="datetime"
            placeholder="选择日期时间"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="预约时长">
          <el-input-number v-model="appointmentForm.duration" :min="30" :step="30" /> 分钟
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="appointmentForm.note" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAppointmentDialog = false">取消</el-button>
        <el-button type="primary" @click="handleAppointment" :loading="submitting">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ChatDotRound, Calendar } from '@element-plus/icons-vue'
import { getSkillDetail, toggleFavorite as toggleFavoriteApi } from '@/api/skill'
import { createAppointment } from '@/api/appointment'

const route = useRoute()
const router = useRouter()

const loading = ref(false)
const submitting = ref(false)
const skill = ref(null)
const showAppointmentDialog = ref(false)

const appointmentForm = ref({
  skillId: null,
  appointmentTime: null,
  duration: 60,
  note: ''
})

const parseTags = (tags) => {
  try {
    return JSON.parse(tags || '[]')
  } catch {
    return []
  }
}

const fetchSkillDetail = async () => {
  loading.value = true
  try {
    const res = await getSkillDetail(route.params.id)
    skill.value = res.data
    appointmentForm.value.skillId = skill.value.id
  } catch (error) {
    console.error('获取技能详情失败：', error)
    ElMessage.error('获取技能详情失败')
    router.back()
  } finally {
    loading.value = false
  }
}

const toggleFavorite = async () => {
  try {
    await toggleFavoriteApi(skill.value.id)
    skill.value.isFavorited = !skill.value.isFavorited
    skill.value.favoriteCount += skill.value.isFavorited ? 1 : -1
    ElMessage.success(skill.value.isFavorited ? '收藏成功' : '取消收藏成功')
  } catch (error) {
    console.error('操作失败：', error)
  }
}

const handleAppointment = async () => {
  if (!appointmentForm.value.appointmentTime) {
    ElMessage.warning('请选择预约时间')
    return
  }
  
  submitting.value = true
  try {
    await createAppointment(appointmentForm.value)
    ElMessage.success('预约成功')
    showAppointmentDialog.value = false
    router.push('/appointment')
  } catch (error) {
    console.error('预约失败：', error)
  } finally {
    submitting.value = false
  }
}

const startChat = () => {
  // 跳转到聊天页面，传入发布者的用户ID
  router.push({
    path: '/chat',
    query: { userId: skill.value.userId }
  })
}

onMounted(() => {
  fetchSkillDetail()
})
</script>

<style scoped>
.skill-detail-container {
  min-height: 100%;
  padding: 30px 20px;
  animation: fadeIn 0.6s ease-out;
}

.detail-content {
  max-width: 1300px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 450px 1fr;
  gap: 30px;
}

.left-section {
  animation: slideInLeft 0.6s ease-out;
}

.right-section {
  animation: slideInRight 0.6s ease-out;
}

.cover {
  width: 100%;
  height: 350px;
  border-radius: 20px;
  overflow: hidden;
  background: linear-gradient(135deg, #f5f7fa 0%, #e8ebf0 100%);
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.15);
  border: 1px solid rgba(102, 126, 234, 0.1);
  position: relative;
}

.cover::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, transparent 0%, rgba(0, 0, 0, 0.3) 100%);
  pointer-events: none;
}

.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.cover:hover img {
  transform: scale(1.05);
}

.info-card {
  background: white;
  padding: 30px;
  border-radius: 20px;
  margin-top: 24px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.12);
  border: 1px solid rgba(102, 126, 234, 0.1);
}

.info-card h1 {
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 20px;
  color: #333;
  line-height: 1.3;
  letter-spacing: -0.5px;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 24px;
}

.tags .el-tag {
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
}

.stats {
  display: flex;
  gap: 24px;
  padding-top: 20px;
  border-top: 1px solid #f0f2f5;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s;
  padding: 8px 12px;
  border-radius: 8px;
}

.stat-item:hover {
  background: rgba(102, 126, 234, 0.08);
  color: #667eea;
}

.stat-item .el-icon {
  font-size: 18px;
}

.description-card, .user-card {
  background: white;
  padding: 30px;
  border-radius: 20px;
  margin-bottom: 24px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.12);
  border: 1px solid rgba(102, 126, 234, 0.1);
  transition: all 0.3s;
}

.description-card:hover, .user-card:hover {
  box-shadow: 0 12px 40px rgba(102, 126, 234, 0.18);
  transform: translateY(-2px);
}

.description-card h2 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 20px;
  color: #333;
  display: flex;
  align-items: center;
  gap: 10px;
}

.description-card h2::before {
  content: '';
  width: 4px;
  height: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.description-card p {
  line-height: 1.8;
  color: #666;
  font-size: 15px;
  white-space: pre-wrap;
}

.user-card h3 {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
  color: #333;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(255, 255, 255, 1) 100%);
  border-radius: 12px;
  margin-bottom: 16px;
}

.user-info .el-avatar {
  border: 3px solid #fff;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.user-details {
  flex: 1;
}

.nickname {
  font-size: 17px;
  font-weight: 700;
  margin-bottom: 6px;
  color: #333;
}

.username {
  font-size: 14px;
  color: #999;
  font-weight: 500;
}

.user-card .el-button {
  border-radius: 12px;
  font-weight: 600;
}

.action-buttons {
  display: flex;
  gap: 16px;
}

.action-buttons .el-button {
  flex: 1;
  font-size: 16px;
  font-weight: 700;
  padding: 20px 0;
  border-radius: 16px;
  transition: all 0.3s;
}

.action-buttons .el-button--primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.action-buttons .el-button--primary:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 32px rgba(102, 126, 234, 0.4);
}

.action-buttons .el-button--default,
.action-buttons .el-button--warning {
  border: 2px solid #667eea;
  color: #667eea;
}

.action-buttons .el-button--default:hover {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
  transform: translateY(-3px);
}

.action-buttons .el-button--warning {
  background: linear-gradient(135deg, #f59e0b 0%, #f97316 100%);
  border: none;
  color: white;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .detail-content {
    grid-template-columns: 1fr;
    gap: 24px;
  }

  .cover {
    height: 300px;
  }
}

@media (max-width: 768px) {
  .skill-detail-container {
    padding: 20px 12px;
  }

  .info-card,
  .description-card,
  .user-card {
    padding: 20px;
    border-radius: 12px;
  }

  .info-card h1 {
    font-size: 22px;
  }

  .action-buttons {
    flex-direction: column;
  }

  .action-buttons .el-button {
    width: 100%;
  }
}
</style>

